package com.sdkj.controller.finance;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.sdkj.controller.base.BaseController;
import com.sdkj.entity.Page;
import com.sdkj.service.finance.FinanceWaterBillsService;
import com.sdkj.service.materials.getmaterials.ApplyGetMaterialsDetailService;
import com.sdkj.service.materials.getmaterials.ApplyGetMaterialsService;
import com.sdkj.service.materials.paymentdocument.ApplyPaymentDocumentService;
import com.sdkj.service.system.GoodTypeService;
import com.sdkj.util.Const;
import com.sdkj.util.PageData;

/** 
 * 类名称：MaterialsFeesConfirmController
 * 创建人：FH 
 * 创建时间：2017-05-25
 */
@Controller
@RequestMapping(value="/materialsfeesconfirm")
public class MaterialsFeesConfirmController extends BaseController {

	String menuUrl = "materialsfeesconfirm/list.do"; //菜单地址(权限用)
	@Resource(name="applygetmaterialsService")
	private ApplyGetMaterialsService applygetmaterialsService;
	@Resource(name="goodtypeService")
	private GoodTypeService goodTypeService;
	@Resource(name="applygetmaterialsdetailService")
	private ApplyGetMaterialsDetailService applygetmaterialsdetailService;
	
	@Resource(name="applypaymentdocumentService")
	private ApplyPaymentDocumentService applypaymentdocumentService;
	
	@Resource(name="financewaterbillsService")
	private FinanceWaterBillsService financewaterbillsService;
	
	/**
	 * 列表
	 */
	@RequestMapping(value="/list")
	public ModelAndView list(Page page){
		logBefore(logger, "列表ApplyGetMaterials");
		//if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			pd.put("userId", getCurrentUser().getUSER_ID());
			String keyword=pd.getString("keyword");
			String lastLoginStart=pd.getString("lastLoginStart");
			String lastLoginEnd=pd.getString("lastLoginEnd");
			String check_state=pd.getString("check_state");
			String pay_state=pd.getString("pay_state");
			String get_state=pd.getString("get_state");
			if(null!=keyword&&!"".equals(keyword)){
				pd.put("keyword", keyword.trim());
			}else{
				pd.put("keyword", null);
			}
			if(null==lastLoginStart || "".equals(lastLoginStart)){
				pd.put("lastLoginStart",null);
			}
			if(null==lastLoginEnd || "".equals(lastLoginEnd)){
				pd.put("lastLoginEnd",null);
			}
			if("-1".equals(check_state)){
				pd.put("check_state",null);
			}
			if("-1".equals(pay_state)){
				pd.put("pay_state",null);
			}
			if("-1".equals(get_state)){
				pd.put("get_state",null);
			}
			page.setPd(pd);
			List<PageData>	varList = applygetmaterialsService.selectmaterialsfeesconfirm(page);	//列出ApplyGetMaterials列表
			if(varList!=null&& varList.size()>0&& varList.get(0)==null){//联表查询无数据处理
				varList=null;
			}
			mv.setViewName("finance/materialsfeesconfirm/applygetmaterials_list");
			mv.addObject("varList", varList);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX,this.getHC());	//按钮权限
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	

	/**
	 * 去详情页面
	 */
	@RequestMapping(value="/goDetail")
	public ModelAndView goDetail(){
		logBefore(logger, "去新增Customer页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		PageData pd1 = new PageData();
		pd1 = this.getPageData();
		try {
			pd = applypaymentdocumentService.findById(pd);	//根据ID读取
			pd1 = financewaterbillsService.findById(pd1);	//根据ID读取
			mv.setViewName("finance/materialsfeesconfirm/waterbillsandapply_detail");
			mv.addObject("msg", "confirmReceipt");
			mv.addObject("pd", pd);
			mv.addObject("pd1", pd1);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}	
	
	
	
	/**
	 * 确认收款方法
	 */
	@RequestMapping(value="/confirmReceipt")
	public ModelAndView confirmReceipt() throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
//		if(Jurisdiction.buttonJurisdiction(menuUrl, "edit")){
			pd.put("pay_state", 3);
			applygetmaterialsService.updateState(pd);
//		}
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	
	
	/* ===============================权限================================== */
	public Map<String, String> getHC(){
		Subject currentUser = SecurityUtils.getSubject();  //shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>)session.getAttribute(Const.SESSION_QX);
	}
	/* ===============================权限================================== */
}
